<template>
    <div class="sysHeader">
        <div class="path">
            <el-avatar :src="logo" :size="40" />
            <div class="title">贵州众创云通科技有限公司</div>
            <el-icon :size="20" color="#ffffff" @click="doHandleMenu">
                <component :is="show ? 'Fold' : 'Expand'"></component>
            </el-icon>
        </div>
        <div class="notice">
            <el-dropdown trigger="click" :teleported="false">
                <div class="userinfo">
                    <span class="username">{{ userInfo.name }}</span>
                    <el-icon :size="18" color="#ffffff">
                        <CaretBottom />
                    </el-icon>
                </div>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item @click="userInfoShow = true">
                            <el-icon>
                                <User />
                            </el-icon>个人信息
                        </el-dropdown-item>
                        <el-dropdown-item @click="passwordShow = true">
                            <el-icon>
                                <Lock />
                            </el-icon>密码修改
                        </el-dropdown-item>
                        <el-dropdown-item @click="doExit">
                            <el-icon>
                                <SwitchButton />
                            </el-icon>退出登录
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>

        <el-dialog v-model="userInfoShow" title="个人信息" top="10vh" width="20%" center draggable>
            <el-form size="default" label-width="60px">
                <el-form-item label="账号:">
                    <span>{{ userInfo.loginAccount}}</span>
                </el-form-item>
                <el-form-item label="姓名:">
                    <span>{{ userInfo.name}}</span>
                </el-form-item>
                <el-form-item label="性别:">
                    <span>{{ userInfo.sex ? '男' : '女' }}</span>
                </el-form-item>
                <el-form-item label="手机号:">
                    <span>{{ userInfo.phone}}</span>
                </el-form-item>
                <el-form-item label="邮箱:">
                    <span>{{ userInfo.email}}</span>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-dialog v-model="passwordShow" title="修改密码" top="10vh" width="20%" center draggable @close="resetForm">
            <el-form ref="formRef" :model="form" :rules="rules" size="large" label-width="85px">
                <el-form-item label="旧密码:" prop="oldPassWord" required>
                    <el-input v-model="form.oldPassWord" type="password" placeholder="请输入旧密码" clearable></el-input>
                </el-form-item>
                <el-form-item label="新密码:" prop="newPassWord" required>
                    <el-input v-model="form.newPassWord" type="password" placeholder="请输入新密码" clearable></el-input>
                </el-form-item>
                <el-form-item label="确认密码:" prop="confirmPassWord" required>
                    <el-input v-model="form.confirmPassWord" type="password" placeholder="请输入确认密码" clearable></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" size="default" @click="doUpdataPassword">修改</el-button>
                <el-button size="default" @click="passwordShow = false">关闭</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="sysHeader">
    import { getLoginUserInfoApi, updateLoginUserPasswordApi } from '@/api/api'
    import logo from '@/assets/logo.jpg'
    import { regexPassword } from '@/utils/utils'
    import { ref, getCurrentInstance } from 'vue'

    const currentThis = getCurrentInstance().appContext.config.globalProperties
    // 获取form表单的ref
    const formRef = ref(null)
    const emit = defineEmits(['handleMenu'])

    let show = ref(false)
    let userInfoShow = ref(false)
    let passwordShow = ref(false)
    let userInfo = ref({
        loginAccount: '',
        name: '',
        sex: '',
        phone: '',
        email: '',
    })
    let form = ref({
        oldPassWord: '',
        newPassWord: '',
        confirmPassWord: ''
    })
    let rules = ref({
        oldPassWord: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
        newPassWord: [{ 
            required: true, trigger: ['blur', 'change'], validator: (rule, value, callback) => {
                if(!value) {
                    callback(new Error('请输入新密码'))
                } else {
                    if(!regexPassword(value)) {
                        callback(new Error('密码为8-20个字符，包含字母，数字，字符,'))
                    } else {
                        callback()
                    }
                }
            } 
        }],
        confirmPassWord: [{ 
            required: true, trigger: ['blur', 'change'], validator: (rule, value, callback) => {
                if(!value) {
                    callback(new Error('请输入确认密码'))
                } else {
                    if(!regexPassword(value)) {
                        callback(new Error('密码为8-20个字符，包含字母，数字，字符,'))
                    } else if(value !== form.value.newPassWord) {
                        callback(new Error('新密码和确认密码不一致'))
                    } else {
                        callback()
                    }
                }
            } 
        }]
    })

    function doHandleMenu() {
        show.value = !show.value
        emit('handleMenu', show.value)
    }

    // 查看登录人信息
    loadUserInfo()
    function loadUserInfo() {
        getLoginUserInfoApi().then(res => {
            if (res.code === '200') {
                userInfo.value = res.data
            } else {
                currentThis.$message.error(res.message)
            }
        })
    }

    // 修改密码
    function doUpdataPassword() {
        formRef.value.validate((valid) => {
            if (valid) {
                currentThis.$messageBox.confirm(`确认修改密码吗？`).then(() => {
                    let loading = currentThis.$loading(`正在修改，请耐心等待`)
                    updateLoginUserPasswordApi({
                        oldPassWord: currentThis.$md5(form.value.oldPassWord),
                        newPassWord: currentThis.$md5(form.value.newPassWord),
                        confirmPassWord: currentThis.$md5(form.value.confirmPassWord)
                    }).then(res => {
                        loading.close()
                        if(res.code === '200') {
                            currentThis.$message.success(res.message)
                            doExit()
                        } else {
                            currentThis.$message.error(res.message)
                        }
                    })
                }).catch(() => {})
            } else {
                return false;
            }
        })
    }

    function doExit() {
        currentThis.$router.push({name: 'login'})
    }

    // 新增编辑弹窗重置数据
    function resetForm() {
        form.value = {
            oldPassWord: '',
            newPassWord: '',
            confirmPassWord: ''
        }
        formRef.value.resetFields()
    }
</script>

<style lang="less" scoped>
.sysHeader {
    width: 100%;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .path {
        display: flex;
        align-items: center;

        .title {
            color: #ffffff;
            font-size: 18px;
            font-weight: bold;
            margin: 0 10px;
        }

        :deep(.el-breadcrumb__item) {
            .el-breadcrumb__inner {
                color: #A3A3A3;
                font-size: 15px;
            }

            .el-breadcrumb__separator {
                margin: 0 4px;
            }
        }

        :deep(.last_breadcrumb) {
            .el-breadcrumb__inner {
                color: #1F1F1F;
            }
        }
    }

    .notice {
        width: 50%;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .badge {
            margin: 12px 25px 0 0;
        }

        .userinfo {
            margin: 0 10px;
            cursor: pointer;
            display: flex;
            align-items: center;

            .avatar {
                margin: 0 10px 0 20px;
            }

            .username {
                color: #ffffff;
                font-size: 16px;
            }
        }
    }

    .el-icon {
        margin: 0 5px;
        cursor: pointer;
    }
}</style>